//导入远程组件
importJsx=[]

//主组件名称
M.mainComponent="App"




////////////////////////////////////////////////////////




let existsIndexHtml= fs.existsSync("./index.html")
const indexHtmlUrl="http://minglie.gitee.io/ming_api_mock_plugins/mi_react_quick_start_plus/index.html"
//下载默认indexHtml
if(!existsIndexHtml){
    M.get(indexHtmlUrl).then(d=>{
        M.writeFile("./index.html",d);
    })

}











importJsxObj={}
importJsx.map(u=> {importJsxObj["/"+path.parse(u).base]=u})

console.log(importJsxObj)

app.begin(async (req,res)=>{
    console.log(req.url)
   if(req.url.endsWith("jsx")||req.url.endsWith("js")){
         let fileContent=""
        if(importJsxObj[req.url]){
            fileContent=await M.getRemoteCacheByUrl(importJsxObj[req.url])
        }else{
            fileContent=M.readFile(staticPath+req.url)
        }
       fileContent= fileContent.replaceAll(/import .*/g,"").replaceAll(/export .*/g,"")
       res.renderJs(fileContent)
       return;
   }
})


function readFileTree(file,fileRealList){
     if(!file.children){
        fileRealList.push(file.value)
     }else{
        file.children.forEach(u=>{
            readFileTree(u,fileRealList)
        })
     }
}

app.get("/index",(req,res)=>{
    fileList=M.getFileList(staticPath)
   // console.log(fileList)
    fileRealList=[]
    fileList.forEach(u => {
        readFileTree(u,fileRealList)
    });
    fileRealList=fileRealList.map(u=>u.replaceAll(staticPath,""))
    fileRealList=[...fileRealList,...Object.keys(importJsxObj)]
    fileList=fileRealList.filter(u=>(u.endsWith("jsx")||u.endsWith("js"))&& !u.endsWith("server.js")).sort().reverse().map(u=>`<script src="${u}" type="text/babel"></script>\n`)
    insertImportJs="";
    for(let i=0;i<fileList.length;i++){
         insertImportJs=insertImportJs+fileList[i];
    }
    res.render("/index.html")
})